package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.base.util.PasswordUtil;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public UserVO login(UserLoginParam param) {
        log.debug("开始处理用户登录业务："+param);
        UserVO userVO=userMapper.selectByUsername(param.getUsername());
        //一旦抛出异常，就会被全局异常处理器拦截到，并由全局异常处理器给前端响应结果
        if (userVO == null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(param.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //只有遇不到上面的两个异常，才会走到下面，由controller层给前端返回结果
        log.debug("查询结果："+userVO);
        return userVO;
    }

    @Override
    public List<UserVO> selectUser(UserQuery query) {
        log.debug("开始处理查询业务："+query);
        List<UserVO> list=userMapper.selectUser(query);
        return list;
    }

    @Override
    public void save(UserSaveParam param) {
        log.debug("开始处理保存业务："+param);
        User user =new User();
        BeanUtils.copyProperties(param,user);
        if (user.getId()==null) {//新增
            user.setCreateTime(new Date());
            user.setPassword("123456");
            userMapper.insertUser(user);
        }else {//更新
            user.setUpdateTime(new Date());
            userMapper.updateUser(user);
        }
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("开始处理更新状态业务："+userId+"--"+status);
        User user =new User();
        user.setId(userId);
        user.setStatus( status);
        user.setUpdateTime(new Date());
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUserId(Long id) {
        userMapper.deleteUserId(id);
    }

    @Override
    public List<UserVO> selectAuditList(Long parentId) {
        log.debug("开始处理查询待审核用户业务："+parentId);
        ArrayList<UserVO> userVO=new ArrayList<>();
        UserVO auditUser1=userMapper.selectById(parentId);
        userVO.add(auditUser1);
        if (auditUser1 !=null && auditUser1.getParentId() !=null){
            UserVO auditUser2=userMapper.selectById(auditUser1.getParentId());
            userVO.add(auditUser2);
        }
        return userVO;
    }

    @Override
    public void updatePassword(Long userId) {
        log.debug("开始处理修改密码业务："+userId);
        User user=new User();
        user.setId(userId);
//        user.setPassword("123456789");
        user.setPassword(PasswordUtil.generateRandomPassword(8));
        user.setUpdateTime(new Date());
//        log.debug(user.getPassword());
        userMapper.updateUser(user);
    }
}
